<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>奖金/罚款排名</title>
		<link rel="stylesheet" href="../static/css/xadmin.css">
		<link rel="stylesheet" href="../static/css/xadmin.css">
		<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
		<script src="../static/lib/layui/layui.js" charset="utf-8"></script>
		<script type="text/javascript" src="../static/js/xadmin.js"></script>
		<script src="../static/js/my.js"></script>
	</head>
	<body>
		<select id="moneyTime" name="moneyTime" style="width: 150px;height: 37px">
		    <option value="">请选择月份</option>
		    <option value="01">1</option>
		    <option value="02">2</option>
			 <option value="03">3</option>
			  <option value="04">4</option>
			   <option value="05">5</option>
			    <option value="06">6</option>
				 <option value="07">7</option>
				 <option value="08">8</option>
				 <option value="09">9</option>
				 <option value="10">10</option>
				 <option value="11">11</option>
				 <option value="12">12</option>
		</select>
		<input type="button" class="layui-btn layui-btn-radius layui-btn-warm" onclick="search()" value="查询" >
		<input type="button" class="layui-btn  layui-btn-radius layui-btn-danger" onclick="add()" value="添加" >
		<table  class="layui-table" id="table">
		    <thead>
		    <th>编号</th>
		    <th>姓名</th>
		    <th>基本工资</th>
			<th>当月绩效</th>
		    <th>奖金</th>
			<th>罚款</th>
			<th>应发工资</th>
			<th>时间</th>
			<th>操作</th>
			
		    </thead>
		    <tbody>
		  
		    </tbody>
		</table>
		
		
		
		
		<!-- 显示页码信息的div -->
		<div align="center">
		    <button type="button" class="layui-btn layui-btn-radius layui-btn-warm"onclick="pre()">
		       <i class="layui-icon">&#xe603;</i>
		     </button>
		     <button type="button" class="layui-btn layui-btn-radius layui-btn-warm"onclick="next()">
		        <i class="layui-icon">&#xe602;</i>
		      </button>
		    一共有<span id="total"></span>条数据
		    一共有<span id="totalPage"></span>页
		    当前第<span id="cur"></span>页
			
		</div>
		
		<input type="button" class="layui-btn layui-btn-radius layui-btn-warm" onclick="count()" value="查看本月奖惩排名" >
		
	</body>
</html>
<script>
	//文档加载事件
	$(function(){
			//在文档加载时查询所有考勤记录
		
		
			selectMoney();
			
		})
	var month;
	var isUpdate="true";
	var page=1;
	var row=6;
	//4.//上一页/下一页
	var prePage=0;
	var nextPage=0;
	//上一页
	function pre()
	{
		page=prePage;
		
		selectMoney();
	
	}
	//下一页
	function next()
	{
		page=nextPage;
		selectMoney();
		
	}
	function selectMoney()
	{
		
		var moneyTime=$("#moneyTime").val();
		$.ajax({
				url:address+"/check/selectAllMoneyAjax",
				type:"get",
				data:{"page":page,"row":row},
				dataType:"json",
				success:function (data) {
				   
				   //遍历分页数据,拼接字符串
				  
				 var html="";
				 for(var i=0;i<data.list.length;i++)
				 {
					 month=data.list[i].moneyTime.split("-")[1];
					 if(month==moneyTime||moneyTime=="")
					 {
						 html+="<tr>"
							+"<td >"+data.list[i].userId+"</td>"
							+"<td >"+data.list[i].userName+"</td>"
							+"<td>"+data.list[i].basicMoney+"</td>"
							+"<td>"+data.list[i].meritMoney+"</td>"
							+"<td>"+data.list[i].addMoney+"</td>"
							+"<td>"+data.list[i].punishMoney+"</td>"
							+"<td>"+data.list[i].totalMoney+"</td>"
							+"<td>"+data.list[i].moneyTime+"</td>"
							+"<td>"
									+"<button class='layui-btn layui-btn-radius layui-btn-warm' type='button' onclick='update(this)'>修改</button>"
									+"<button class='layui-btn  layui-btn-radius layui-btn-danger' type='button' onclick='delUser(this)'>删除</button>"
							+"</td>"
						  +"</tr>"
					  }
				 }
				    $("tbody").html(html);
				    
				    $("#total").text(data.total);
				    $("#totalPage").text(data.totalPage);
				    $("#cur").text(data.cur);
				   prePage=data.pre;
				   nextPage=data.next;
				
				    }
			})
	}
	
	//修改与保存修改:
	function update(obj)
	{
		
		if(isUpdate=="true")
		{
			$(obj).text("保存");
			for(var i=2;i<6;i++)
			{
				$(obj).parent().parent().find("td").eq(i).attr("contenteditable","true");
				
			}
			isUpdate="false";
		}else
		{
			saveUpdate(obj);
			$(obj).text("修改");
			for(var i=2;i<6;i++)
			{
				$(obj).parent().parent().find("td").eq(i).attr("contenteditable","false");
				
			}
			//刷新
			window.location.reload();
			isUpdate="true";
		}
		
		
	}
	function saveUpdate(obj)
	{
		//用于数据库判断:
		var userId=$(obj).parent().parent().find("td").eq(0).text();
	
	    //用户修改的数据:
		var basicMoney=$(obj).parent().parent().find("td").eq(2).text();
		var meritMoney=$(obj).parent().parent().find("td").eq(3).text();
		var addMoney=$(obj).parent().parent().find("td").eq(4).text();
		var punishMoney=$(obj).parent().parent().find("td").eq(5).text();
		var totalMoney=$(obj).parent().parent().find("td").eq(6).text();
		
		
		var moneyTime=$(obj).parent().parent().find("td").eq(7).text();
		$.ajax({
				url:address+"/check/updateMoneyAjax",
				type:"get",
				data:{"page":page,"row":row,"userId":userId,"moneyTime":moneyTime,
				"basicMoney":basicMoney,"meritMoney":meritMoney,"addMoney":addMoney,
				"punishMoney":punishMoney,"totalMoney":totalMoney},
				dataType:"json",
				success:function (data) {
				   
				  if(data.info=="成功")
				        // layer.close(layer.index);
				  window.alert("修改成功！");
				
				    }
			})
	}
	 
	
	
	//删除:
	function delUser(obj)
	{
		//获取当前选中行的userId,moneyTime值
		           var id = $(obj).parent().parent().find("td").eq(0).text();
				   var time = $(obj).parent().parent().find("td").eq(7).text();
				 
		           //用提示框确定用户是否删除
		           layer.confirm("确定要删除工资记录吗?",{icon:3,title:"警告"},function(index){
		               $.ajax({
		                   url:address+"/check/delUserAjax",
		                   type:"get",
		                   data:{"userId":id,"moneyTime":time},
		                   dataType:"json",
		                   success:function(data){
		                       layer.alert(data.info,function(){
								  
									    window.location.href="money.html";
								  
		                          
		                       })
		             /*          if(data.info=="删除成功"){
		                           // 第一种：刷新
		                           //parent.window.location.href=parent.window.location.href;
		                           //第二种：刷新
		                           $(obj).parent().parent().remove();
		                       }*/
		                       //关闭
		                       layer.close(index);
		                   }
		               })
		   
		           })
	}
	
	
	//添加
	function add()
	{
		var html="";
		html= html+="<tr>"
			+"<td contenteditable='true'></td>"
			+"<td contenteditable='true'></td>"
			+"<td contenteditable='true'></td>"
			+"<td contenteditable='true'></td>"
			+"<td contenteditable='true'></td>"
			+"<td contenteditable='true'></td>"
			+"<td contenteditable='true'></td>"
			+"<td contenteditable='true'></td>"
			+"<td>"
					+"<button class='layui-btn layui-btn-radius layui-btn-warm' type='button' onclick='save(this)'>保存</button>"
					+"<button class='layui-btn layui-btn-radius layui-btn-danger' type='button' onclick='delUser(this)'>删除</button>"
			+"</td>"
		  +"</tr>"
		  
		  $("#table").append(html);
	}
	
	
	//数据默认为0:
	var userId="";
	var userName="";
	var basicMoney=0;
	var meritMoney= 0;
	var addMoney=0;
	var punishMoney=0;
	var moneyTime="";
	function save(obj)
	{
		
			 userId=$(obj).parent().parent().find("td").eq(0).text();
			 userName=$(obj).parent().parent().find("td").eq(1).text();
			 basicMoney=$(obj).parent().parent().find("td").eq(2).text();
			 meritMoney=$(obj).parent().parent().find("td").eq(3).text();
			 addMoney=$(obj).parent().parent().find("td").eq(4).text();
			 punishMoney=$(obj).parent().parent().find("td").eq(5).text();
			
			 moneyTime=$(obj).parent().parent().find("td").eq(7).text();
			$.ajax({
					url:address+"/check/addAjax",
					type:"get",
					data:{"page":page,"row":row,"userId":userId,"userName":userName, "moneyTime":moneyTime,
					"basicMoney":basicMoney,"meritMoney":meritMoney,"addMoney":addMoney,
					"punishMoney":punishMoney},
					dataType:"json",
					success:function (data) {
					   
					  if(data.info=="成功")
							// layer.close(layer.index);
					  window.alert("添加成功！");
					window.location.reload();
						}
				})
			
		
	}
	
	function search()
	{
		selectMoney();
	}
	
	function count()
	{
		window.location.href="count.html";
	}
	
	
</script>